package com.compile;

import java.io.IOException;
//import java.util.Scanner;

public class Calculator {

	public static void main(String[] args) throws IOException {

//        Scanner scanner = new Scanner(System.in);

        String value = "";
        Node lastNode = null;
        while(true) {
            char cmd = (char) System.in.read();
            if(cmd == 'q' || cmd == 'Q')
                return;
            switch (cmd) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    value += cmd;
                    continue;
                case '+':
                case '-':
                case '*':
                case '/':
//                    if(lastNode != null && (value == null || value.length() <= 0)) {
                    if(value == null || value.length() <= 0) {
                        System.out.println("当前需要输入数值！");
                        continue;
                    }

                	System.out.print(value);
                    ValueNode valueNode = new ValueNode(Integer.parseInt(value));
                    value = "";
                    if(lastNode != null) {
                        lastNode.insert(valueNode);
                    }
                    lastNode = valueNode;
                    
                    System.out.print(cmd);
                    OperationNode operationNode = new OperationNode(cmd);
                    lastNode.insert(operationNode);
                    lastNode = operationNode;
                    continue;
                case '=':
                	if(value == null || value.length() <= 0) {
                        System.out.println("当前需要输入数值！");
                        continue;
                    }
                	
                	System.out.print(value);
                    ValueNode valueNode1 = new ValueNode(Integer.parseInt(value));
                    value = "";
                    if(lastNode != null) {
                        lastNode.insert(valueNode1);
                    }
                    lastNode = valueNode1;
                	
                    // 计算结果
                    System.out.println("=" + lastNode.root().value());
                    lastNode = null;
                    continue;
            }

        }
    }



    // 叶子节点
    public static abstract class Node {
        // 父节点
        protected Node parent;
        // 左叶子
        protected Node nodeL;
        // 右叶子
        protected Node nodeR;


        private final int priority;

        protected Node(int priority) {
            this.priority = priority;
        }

        public Node root() {
            if(parent != null)
                return parent.root();
            return this;
        }

        public void insert(Node node) {
            if(priority >= node.priority)
            {
                node.parent = this;
                if(nodeL == null) {
                    nodeL = node;
                } else {
                    nodeR = node;
                }
            } else {
            	if(parent != null)
            		parent.insert(node);
                node.insert(this);
            }
        }

        public abstract int value();
    }

    // 运算符节点
    public static class OperationNode extends Node {

        private final char operation;
        
        private static int priority(char operation) {
        	switch(operation)
        	{
        		case '+':
        		case '-':
        			return 2;
        		case '*':
        		case '/':
        			return 1;
        	}
        	throw new RuntimeException("非法得运算符！！！！！");
        }
        
        protected OperationNode(char operation) {
            super(priority(operation));
            this.operation = operation;
        }

        @Override
        public int value() {
            switch (operation)
            {
                case '+' :
                    return nodeL.value() + nodeR.value();
                case '-' :
                    return nodeL.value() - nodeR.value();
                case '*' :
                    return nodeL.value() * nodeR.value();
                case '/' :
                    return nodeL.value() / nodeR.value();
            }
            throw new RuntimeException("非法得运算符！！！！！");
        }
    }

    // 操作数节点
    public static class ValueNode extends Node {

        private final int value;

        public ValueNode(int value) {
            super(0);
            this.value = value;
        }

        @Override
        public int value() {
            return value;
        }
    }
	
}
